I am trying to figure out how to take a string that is the file contents of a mp4 file and write a properly formatted mp4 file. Currently I am just throwing the string into a file and slapping a .mp4 extension on it, but this resulting file cannot be played by any video players (I am assuming because of all the missing meta data).
def write_mp4(mp4_string)
file = File.new('movie.mp4', 'w')
file.puts(mp4_string)
file.close
end
For context, I am doing this in a Ruby on Rails application, not sure if that changes anything. Please help thanks.
Use "wb" mode, which will suppress EOL conversions and set the encoding properly
Use write, not puts, as the latter inserts an extra EOL
You could use a shortcut: File.write('movie.mp4', mp4_string, mode: 'wb') - or even File.binwrite('movie.mp4', mp4_string).
Of course, make sure the string actually contains a correct file before - for example, if mp4_string.encoding doesn't return #<Encoding:ASCII-8BIT>, you probably done goofed somewhere before the writing step, too :)
Related
In a model, I want to get a file's contents without rendering it. Say the file is a .erb file. I want to store its contents in a database, and then later on, I'll evaluate the string so that it replaces the variables in the .erb file with actual values.
Is there a method like render_to_string that doesn't actually evaluate the .erb part?
I'll ignore any reason you would want to do something like this. Ok so, you're looking to read files, this is done with plain Ruby:
File.read 'path/to/file'
That's how you read any file in Ruby. For a view in Rails you'd have to specify the path:
File.read Rails.root.join('app/views/some_view_dir/your_view_file.erb')
Just replace some_view_dir/your_view_file.erb with your actual view.
Reading files this way gets just the raw content, and you can do this with any file type.
I am trying to parse a CSV file generated from an Excel spreadsheet.
Here is my code
require 'csv'
file = File.open("input_file")
csv = CSV.parse(file)
But I get this error
ArgumentError: invalid byte sequence in UTF-8
I think the error is because Excel encodes the file into ISO 8859-1 (Latin-1) and not in UTF-8
Can someone help me with a workaround for this issue, please
Thanks in advance.
You need to tell Ruby that the file is in ISO-8859-1. Change your file open line to this:
file=File.open("input_file", "r:ISO-8859-1")
The second argument tells Ruby to open read only with the encoding ISO-8859-1.
Specify the encoding with encoding option:
CSV.foreach(file.path, headers: true, encoding:'iso-8859-1:utf-8') do |row|
...
end
You can supply source encoding straight in the file mode parameter:
CSV.foreach( "file.csv", "r:windows-1250" ) do |row|
<your code>
end
If you have only one (or few) file, so when its not needed to automatically declare encoding on whatever file you get from input, and you have the contents of this file visible in plaintext (txt, csv etc) separated with i.e. semicolon, you can create new file with .csv extension manually, and paste the contents of your file there, then parse the contents like usual.
Keep in mind, that this is a workaround, but in need of parsing in linux only one big excel file, converted to some flavour of csv, it spares time on experimenting with all those fancy encodings
Save the file in utf-8, unless for some reason you need to save it differently in which case you may specify the encoded set while reading the file
add second argument "r:ISO-8859-1" as File.open("input_file","r:ISO-8859-1" )
I had this same problem and was just using google spreadsheets and then downloading as a CSV. That was the easiest solution.
Then I came across this gem
https://github.com/singlebrook/utf8-cleaner
Now I don't need to worry about this issue at all. Hope this helps!
I have a collection of daily MP3s that I'm expected to upload to a web server once a month — they're named with a consistent format (ex. 10-17-11 Always Expect the Best # 1.mp3) and for each file, I have to generate an .m3u file with the URL to the web-server link. At the moment, I manually create each .m3u file and save it in relation to the MP3.
There must be a way to generate the .m3u files automatically — they usually are in the format of http://url/audio/2011/10-17-11_.mp3. I've created a plain text list of each filename on a separate line — if possible, I'd like to take that list and parse it into individual .m3u files.
I'm not sure what I should be using to do this — Python, Ruby, maybe just AppleScript?
Any help is greatly appreciated. Thanks!
you probably could do it with any of those, i'll give you a few pointers with python:
with open("my_file","r") as fin: ## open a file for reading
i = 0
for line in fin: ## iterate through all lines
newline = line + line.split(" ")[0] ## create a new line
with open("output"+str(i),"w") as fout: ## open a file for writing
fout.write(newline) ## write...
i+=1
This script reads a file and for each line appends the first word and writes it to its own file.
You can modify the loop code to aggregate several lines each time and write them to a file. And you can extract information from the line you read and use it to construct the new line.
I am trying to upload a file in rails (using paperclip), and I want to process some of the file data before letting paperclip send it off to s3 storage. In my controller, I just grab the file parameter (which does give me a file) and then I try to read the lines into an array
csv_file = params[:activity][:data]
array = IO.readlines(csv_file.path)
The problem is, I'm only getting the last line of the file. I tried using .rewind, but still get just the last line.
I dislike readlines and I always use regular expressions. Try this.
End of line - \n
Handy block structure to ensure that the file handle is closed:
File.open(csv_file.path) do |f|
a = f.readlines
process a...
end
Reading a whole file into memory might not be a good idea depending on the size of the files.
I upload/save mp3 files through Paperclip, it transforms the name with underscores when it saves it.
For example if I upload "Gould Stokowski 1.mp3" it saves into the the db as "Gould_Stokowski_1.mp3". How can I take out the underscores (replace them with spaces" when I retrieve the file and I want to display the name.
What does the program do with the characters that started out as underscores? If it does nothing, then there is no way to go back using just the file name. The names don't "round trip."
If you're not concerned with that, then your question really has nothing to do with Paperclip or MP3 files at all. You just need to know how to change all the underscores into spaces. You can use String#tr for that:
$ irb
>> "Gould_Stokowski_1.mp3".tr('_', ' ')
=> "Gould Stokowski 1.mp3"